package com.gallsoft.amidar;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;

public class Amidar extends Activity {
	private GLSurfaceView mGLSurfaceView;
    private Game mGame;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        DebugLog.d("Amidar", "onCreate");
        setContentView(R.layout.main);
        mGLSurfaceView = (GLSurfaceView) findViewById(R.id.glsurfaceview);
        
        mGLSurfaceView.setEGLConfigChooser(false); // 16 bit, no z-buffer
        
        mGame = new Game();
        mGame.setSurfaceView(mGLSurfaceView);
        
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        int defaultWidth = 320;
        int defaultHeight = 480;
        
        if (dm.widthPixels != defaultWidth) {
        	float ratio =((float)dm.widthPixels) / dm.heightPixels;
        	defaultWidth = (int)(defaultHeight * ratio);
        }
        
        mGame.bootstrap(this, dm.widthPixels, dm.heightPixels, defaultWidth, defaultHeight);
        mGLSurfaceView.setRenderer(mGame.getRenderer());

    }
    
    protected void onDestroy() {
        DebugLog.d("Amidar", "onDestroy()");
        mGame.stop();
 
        super.onDestroy();        
    }

    protected void onPause() {
        super.onPause();
        DebugLog.d("Amidar", "onPause");

        //hidePauseMessage();
        
        mGame.onPause();
        mGLSurfaceView.onPause();
        mGame.getRenderer().onPause();	// hack!        
    }
    
    protected void onResume() {
        super.onResume();
        
        DebugLog.d("Amidar", "onResume");
        mGLSurfaceView.onResume();

    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	boolean result = true;
    	
	    result = mGame.onKeyDownEvent(keyCode);
	    
	    // Sleep so that the main thread doesn't get flooded with UI events.
	    try {
	        Thread.sleep(4);
	    } catch (InterruptedException e) {
	        // No big deal if this sleep is interrupted.
	    }
	
	    return result;
    }
    
    public boolean onKeyUp(int keyCode, KeyEvent event) {
    	boolean result = false;
    	
		result = mGame.onKeyUpEvent(keyCode);
		
        // Sleep so that the main thread doesn't get flooded with UI events.
        try {
            Thread.sleep(4);
        } catch (InterruptedException e) {
            // No big deal if this sleep is interrupted.
        }
	
        return result;

    }}